<template>
  <div class="app-container">
    <span style="font-size: large">总分成：{{ this.commission}} 元</span>
    <el-tabs v-model="status" @tab-click="handleClick">
      <el-tab-pane label="全部" name="-10"></el-tab-pane>
    </el-tabs>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="用户id" width="110">
        <template slot-scope="scope">
          {{ scope.row.userId }}
        </template>
      </el-table-column>
      <el-table-column label="昵称" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="头像" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
          />
        </template>
      </el-table-column>
      <el-table-column label="昵称" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="充值金额" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.rechargeNumber }}
        </template>
      </el-table-column>
      <el-table-column label="佣金" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.commission }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />

    <el-dialog title="学生审核" :visible.sync="dialogFormVisible">
      <div v-if="form.status==-1">
        <el-input
          v-model="form.reason"
          type="textarea"
          :rows="2"
          placeholder="请输入拒绝理由"
        />
      </div>
      <el-radio v-model="form.status" label="1">通过</el-radio>
      <el-radio v-model="form.status" label="-1">不通过</el-radio>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="verifyStudent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, verify , getTotalCommission} from '@/api/student'
import {getUser} from "@/utils/auth";
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      form: {
        id: 0,
        reason: '',
        status: 0
      },
      dialogFormVisible: false,
      list: null,
      commission: 0,
      listLoading: true,
      status: '-10',
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.fetchData(this.page);
  },
  methods: {
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      const user = getUser();
      const json = JSON.parse(user);
      console.log('json: ',json)
      getList({ page: this.page, size: this.size, phone: json.phone }).then(response => {
        this.total = response.total
        this.list = response.data
        this.listLoading = false
      })
      this.getCommission(json.phone);
    },
    getCommission(val) {
      getTotalCommission({ phone: val}).then(res => {
        this.commission = res.data
        console.log('data', res.data)
      })
    },
    verifyStudent() {
      this.dialogFormVisible = false;

      this.listLoading = true
      verify(this.form).then(res => {
        this.fetchData(this.page)
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        console.log(res)
      }).catch(error => {
        this.$message({
          message: `操作失败！错误信息：${error}`,
          type: 'error'
        })
      })
    }
  }
}
</script>
